﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ConstantCMapEditor3
{
    public partial class BatchChangeForm : Form
    {
        public BatchChangeForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog c = new ColorDialog();

            if (c.ShowDialog() == DialogResult.OK) 
            {
                numericUpDownR.Value = c.Color.R;
                numericUpDownG.Value = c.Color.G;
                numericUpDownB.Value = c.Color.B;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<Command> list = new List<Command>();

            foreach (Declaration d in ThreeLayerSpriteScene.controlList) 
            {
                list.Add(new ColorCommand(d.sprite,new Microsoft.Xna.Framework.Color((int)numericUpDownR.Value, (int)numericUpDownG.Value, (int)numericUpDownB.Value,(int)numericUpDownA.Value)));
            }

            Game1.commandManager.Execute(new BatchCommand(list));
        }

        private void button3_Click(object sender, EventArgs e)
        {

            List<NormalSprite> spriteList= new List<NormalSprite>() ;

            foreach(Declaration d in ThreeLayerSpriteScene.controlList)
            {
            spriteList.Add((NormalSprite)d);
            }

            if (ThreeLayerSpriteScene.controlList.Count > 0) 
            {
                Game1.commandManager.Execute(new BatchRotateTransform(spriteList, (double)numericUpDownRotate.Value));
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<NormalSprite> Normal = new List<NormalSprite>();

            foreach (Declaration d in ThreeLayerSpriteScene.controlList)
            {
                if (d is NormalSprite) 
                {
                    Normal.Add(((NormalSprite)d));
                }
            }

            if (Normal.Count > 0) 
            {
                Game1.commandManager.Execute(new BatchMirrorTransform(Normal));
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {

            List<Command> list = new List<Command>();

            foreach (Declaration d in ThreeLayerSpriteScene.controlList)
            {
                if (d is NormalSprite)
                {
                    list.Add(new SpriteUnLockCommand((NormalSprite)d));
                }
            }

            if (list.Count > 0) 
            {
                Game1.commandManager.Execute(new BatchCommand(list));
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            List<Command> list = new List<Command>();

            foreach (Declaration d in ThreeLayerSpriteScene.controlList)
            {
                if (d is NormalSprite)
                {
                    list.Add(new SpriteLockCommand((NormalSprite)d));
                }
            }

            if (list.Count > 0)
            {
                Game1.commandManager.Execute(new BatchCommand(list));
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (Game1.Selected != null) 
            {
                Game1.Selected.InstanceInit();
                Declaration temp = Game1.declarationList[Game1.declarationList.Count - 1];
                temp.sprite.SpriteColor = Game1.Selected.sprite.SpriteColor;
                temp.RotateR = Game1.Selected.RotateR;
           

                if (numericUpDownDistX.Value % 32 != 0 || numericUpDownDistY.Value % 32 != 0) 
                {
                    if (temp is NormalSprite) ((NormalSprite)temp).LockAt32 = false;
                }

                temp.Position = Game1.Selected.Position + new Vector2((float)numericUpDownDistX.Value, (float)numericUpDownDistY.Value);

                Game1.Selected = temp;
            }
            
        }

        private void button8_Click(object sender, EventArgs e)
        {
            List<NormalSprite> spriteList = new List<NormalSprite>();

            foreach (Declaration d in ThreeLayerSpriteScene.controlList)
            {
                spriteList.Add((NormalSprite)d);
            }

            if (ThreeLayerSpriteScene.controlList.Count > 0)
            {
                Game1.commandManager.Execute(new BatchRotateTransform(spriteList,Math.PI/2));
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (!Game1.WorkSpaceLock || !Game1.IsSelecting)
            {

                List<NormalSprite> TargetList = new List<NormalSprite>();

                for (int i = 0; i < ThreeLayerSpriteScene.controlList.Count; i++)
                {
                    if (ThreeLayerSpriteScene.controlList[i] is NormalSprite)
                    {
                        TargetList.Add((NormalSprite)ThreeLayerSpriteScene.controlList[i]);
                    }
                }

                if (Game1.Selected != null && Game1.Selected is NormalSprite)
                {
                    TargetList.Add((NormalSprite)Game1.Selected);
                }

                Game1.Selected = null;
                ThreeLayerSpriteScene.controlList.Clear();


                Game1.commandManager.Execute(new ChangeLayerCommand(TargetList, Game1.SpriteEditorScene.LayerEditState, LayerState.Layer1));
            }
            

        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (!Game1.WorkSpaceLock || !Game1.IsSelecting)
            {

                List<NormalSprite> TargetList = new List<NormalSprite>();

                for (int i = 0; i < ThreeLayerSpriteScene.controlList.Count; i++)
                {
                    if (ThreeLayerSpriteScene.controlList[i] is NormalSprite)
                    {
                        TargetList.Add((NormalSprite)ThreeLayerSpriteScene.controlList[i]);
                    }
                }

                if (Game1.Selected != null && Game1.Selected is NormalSprite)
                {
                    TargetList.Add((NormalSprite)Game1.Selected);
                }

                Game1.Selected = null;
                ThreeLayerSpriteScene.controlList.Clear();
                Game1.commandManager.Execute(new ChangeLayerCommand(TargetList, Game1.SpriteEditorScene.LayerEditState, LayerState.Layer2));
            }
            
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (!Game1.WorkSpaceLock || !Game1.IsSelecting)
            {
                List<NormalSprite> TargetList = new List<NormalSprite>();

                for (int i = 0; i < ThreeLayerSpriteScene.controlList.Count; i++)
                {
                    if (ThreeLayerSpriteScene.controlList[i] is NormalSprite)
                    {
                        TargetList.Add((NormalSprite)ThreeLayerSpriteScene.controlList[i]);
                    }
                }

                if (Game1.Selected != null && Game1.Selected is NormalSprite)
                {
                    TargetList.Add((NormalSprite)Game1.Selected);
                }

                Game1.Selected = null;
                ThreeLayerSpriteScene.controlList.Clear();

                Game1.commandManager.Execute(new ChangeLayerCommand(TargetList, Game1.SpriteEditorScene.LayerEditState, LayerState.Layer3));
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (!Game1.WorkSpaceLock || !Game1.IsSelecting)
            {
                List<NormalSprite> TargetList = new List<NormalSprite>();

                for (int i = 0; i < ThreeLayerSpriteScene.controlList.Count; i++)
                {
                    if (ThreeLayerSpriteScene.controlList[i] is NormalSprite)
                    {
                        TargetList.Add((NormalSprite)ThreeLayerSpriteScene.controlList[i]);
                    }
                }

                if (Game1.Selected != null && Game1.Selected is NormalSprite)
                {
                    TargetList.Add((NormalSprite)Game1.Selected);
                }

                Game1.Selected = null;
                ThreeLayerSpriteScene.controlList.Clear();
                    Game1.commandManager.Execute(new ChangeLayerCommand(TargetList, Game1.SpriteEditorScene.LayerEditState, LayerState.Layer4));
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (!Game1.WorkSpaceLock||!Game1.IsSelecting)
            {
                List<NormalSprite> TargetList = new List<NormalSprite>();

                for (int i = 0; i < ThreeLayerSpriteScene.controlList.Count; i++)
                {
                    if (ThreeLayerSpriteScene.controlList[i] is NormalSprite)
                    {
                        TargetList.Add((NormalSprite)ThreeLayerSpriteScene.controlList[i]);
                    }
                }

                if (Game1.Selected != null && Game1.Selected is NormalSprite)
                {
                    TargetList.Add((NormalSprite)Game1.Selected);
                }

                Game1.Selected = null;
                ThreeLayerSpriteScene.controlList.Clear();

                    Game1.commandManager.Execute(new ChangeLayerCommand(TargetList, Game1.SpriteEditorScene.LayerEditState, LayerState.Layer5));
            }
        }

        private void BatchChangeForm_Load(object sender, EventArgs e)
        {

        }
    }
}
